package main.java.FSMDE.persistence;


import main.java.FSMDE.models.InterfaceCollection;
import main.java.FSMDE.models.Transition;
import main.java.FSMDE.models.TransitionCollection;
import main.java.FSMDE.persistence.TransitionXmlManager;
import main.java.common.IXmlManager;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class TransitionCollectionXmlManager implements
		IXmlManager<TransitionCollection> {

	private InterfaceCollection interfaceCollection;

	public TransitionCollectionXmlManager(
			InterfaceCollection interfaceCollection) {
		this.interfaceCollection = interfaceCollection;
	}

	public Element getElementFromItem(TransitionCollection collection,
			Document document) {
		Element element = document.createElement("transitions");

		for (Transition transition : collection) {
			element.appendChild(new TransitionXmlManager()
					.getXMLElementFromItem(transition, document));
		}
		return element;
	}

	public TransitionCollection getItemFromXmlElement(Element transitionsElement)
			throws Exception {
		TransitionCollection transitions = new TransitionCollection();

		if (transitionsElement == null)
			return transitions;
		NodeList transitionList = transitionsElement
				.getElementsByTagName("transition");

		for (int i = 0; i < transitionList.getLength(); i++) {
			Element transitionElement = (Element) transitionList.item(i);
			transitions.add(new TransitionXmlManager().getItemFromXmlElement(
					transitionElement, this.interfaceCollection));
		}
		return transitions;
	}
}
